//
//  WmcDelayBtn.swift
//  YPWatermarkCamera
//
//  Created by 赵新 on 2022/3/3.
//

import UIKit

extension WmcDelayBtn {
    /// 延时拍照的时间
    enum DelayTime: Int, CaseIterable {
        case none = 0
        case five = 1
        case ten = 2

        struct Info {
            var time: Int
            var title: String
            var image: UIImage
        }

        /// 信息
        var info: Info {
            switch self {
            case .none: return .init(time: 0, title: "延时拍照", image: "icon_delay_32".wmc_image)
            case .five: return .init(time: 5, title: "延时5s", image: "icon_delay_5s_32".wmc_image)
            case .ten: return .init(time: 10, title: "延时10s", image: "icon_delay_10s_32".wmc_image)
            }
        }
    }
}

// MARK: - [WmcDelayBtn]

/// 延时拍照按钮
class WmcDelayBtn: WmcTopImgBottomTextBtn {
    /// 事件Block
    var actionBlock: YPBlock.Action<WmcDelayBtn>?

    /// 正常颜色
    var normalColor: UIColor = .white
    /// 选中颜色
    var selectColor: UIColor = .wmc_mainTheme
    
    /// 延时类型
    var delayTime: DelayTime = .none {
        didSet {
            self.title = delayTime.info.title
            self.imageView.image = delayTime.info.image
            if delayTime == .none {
                self.imageView.yp.set(tintColor: normalColor)
                self.textColor = normalColor
            }else{
                self.imageView.yp.set(tintColor: selectColor)
                self.textColor = selectColor
            }
        }
    }

    static func Create() -> WmcDelayBtn {
        let btn = WmcDelayBtn()
        btn.font = 12.wmc_font
        btn.imgTextSpacing = 2
        btn.imgSize = 24
        btn.yp.addEvent { btn in
            YPDevice.PlayShake(.heavy)
            btn.clickDelay()
            btn.actionBlock?(btn)
        }
        btn.addTarget(btn, action: #selector(btn._touchDown), for: .touchDown)
        btn.delayTime = .none
        return btn
    }
    
    @objc func _touchDown() {
        YPDevice.PlayShake(.medium)
    }

    /// 递增事件
    func clickDelay() {
        let new = delayTime.rawValue
        if let newCase = DelayTime(rawValue: new + 1) {
            delayTime = newCase
        } else {
            delayTime = .none
        }
    }
}
